﻿using CallCenter.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CallCenter.Data.Base;
using CallCenter.Data.Utility;
using System.IO;

namespace CallCenter.Web.Controllers
{
    public class MenuAccessController : BaseController
    {
        //
        // GET: /MenuAccess/
        public ActionResult Index()
        {
            List<RoleModelVM> roleModelVmList = this.Bridge.Role.GetRoles();
            List<MenuVM> MenuVMList = this.Bridge.MenuUtility.GetAllMenuByRoleID(1);
            ViewBag.RoleId = new System.Web.Mvc.SelectList(roleModelVmList, "Id", "RoleName");
            return View(MenuVMList);
        }

        public string ChangeStatus(int RoleId, int MenuID, string Status)
        {
            return this.Bridge.MenuUtility.GetAllMenuByRoleID(RoleId, MenuID, Status);
        }

        public string GetMenuContentByRole(int RoleId)
        {
            List<MenuVM> MenuVMList = this.Bridge.MenuUtility.GetAllMenuByRoleID(RoleId);
            string RenderString = RenderRazorViewToString("~/Views/MenuAccess/_ParentMenu.cshtml", MenuVMList);
            return RenderString;
        }

        public string RenderRazorViewToString(string viewName, object model)
        {
            ViewData.Model = model;
            using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext,
                                                                         viewName);
                var viewContext = new ViewContext(ControllerContext, viewResult.View,
                                             ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);
                viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
                return sw.GetStringBuilder().ToString();
            }
        }

	}
}